﻿<?php require_once("includes/session.php"); ?>
<?php confirm_logged_in(); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>

<?php
if (isset($_POST['Submit'])){
	if (isset($_POST['laikotarpis']) && !empty($_POST['laikotarpis'])){
		//echo "Vardas: " . $_POST['vardas']. "</br>";
		$laikotarpis = $_POST['laikotarpis'];
	};	
	
	if (isset($_POST['vardas']) && !empty($_POST['vardas'])){
		//echo "Vardas: " . $_POST['vardas']. "</br>";
		$vardas = $_POST['vardas'];
	};	
	if (isset($_POST['pavarde']) && !empty($_POST['pavarde'])){
		//echo "Pavarde: " . $_POST['pavarde']. "</br>";
		$pavarde = $_POST['pavarde'];
	};
	if (isset($_POST['bylosnr']) && !empty($_POST['bylosnr'])){
		//echo "Bylos Nr: " . $_POST['bylosnr']. "</br>";
		$bylosnr = $_POST['bylosnr'];
	};	
	if (isset($_POST['etape']) && !empty($_POST['etape'])) {
	$etape = "1";
	} else {
	$etape = "0";
	};

	// Normalus query:

	$sql = "SELECT * ";
	$sql .= "FROM `user` ";
	$sql .= "WHERE `user`.etapas = ";
	$sql .= $etape;
	if (isset($_POST['bylosnr']) && !empty($_POST['bylosnr'])){
			//echo "Bylos Nr: " . $_POST['bylosnr']. "</br>";
			$sql .= " AND `user`.bylosnr LIKE '%" . $_POST['bylosnr'] . "%'";
		};	
	//Tikrinam ar yra yrashytas vardas
	if (isset($_POST['vardas']) && ($_POST['vardas']) != null ){
		//echo "</br> YRA VARDAS </br>";
		$sql .= " AND `user`.vardas LIKE ";
		$sql .= "'%".$_POST['vardas']."%'";
	} else { 
		//echo "</br> NERA VARDO </br>";
	};
	//Tikrinam ar yra yrasyta pavarde
	if (isset($_POST['pavarde']) && ($_POST['pavarde']) != null ){
		//echo "</br> YRA pavarde </br>";
		$sql .= " AND `user`.pavarde LIKE ";
		$sql .= "'%".$_POST['pavarde']."%'";
	} else { 
		//echo "</br> NERA Pavardes </br>";
	};
	$sql .= " ORDER BY `user`.`pavarde` ASC";

	
//echo "SQL query: ".$sql . "</br>";
	//Bandom procesinti query
	$result = mysql_query($sql, $connection);
	if ($result) {
		// Success!
		//echo "Successs!! </br>";
	} else {
		// Display error message.
		echo "<p>Subject creation failed.</p>";
		echo "<p>" . mysql_error() . "</p>";
	};
	//xz kas bus
	//$subject = mysql_fetch_array($result);
	//print_r ($subject) ;

				// kazkaip formuotas table'as
			$out = "<table border=\"1\">";
			$out .= "<tr>";
			$out .= "<th>Name</th>";
			$out .= "<th>Surname</th>";
			$out .= "<th>Bylos Nr.</th>";
			$out .= "</tr>";
					  
		 while ($row_result = mysql_fetch_assoc($result)) { 
			$out .= "<tr";
			if (($row_result['etapas']) == 0) {
				$out .=" bgcolor=\"#DDFFCC\"";
			} elseif (($row_result['etapas']) == 1) {
				$out .=" bgcolor=\"#FFFBC1\"";
			};
			$out .= ">";
			$out .= "<td>";
			$out .= "<a href=\"ismokos.php?laikotarpis=";
			$out .= $laikotarpis;
			$out .= "&userid=";
			$out .= $row_result['userid'];
			$out .= "\">";
			$out .= $row_result['vardas'];
			$out .= "</a>";
			$out .= "</td>";
			$out .= "<td>";
			$out .= "<a href=\"ismokos.php?laikotarpis=";
			$out .= $laikotarpis;
			$out .= "&userid=";
			$out .= $row_result['userid'];
			$out .= "\">";
			$out .= $row_result['pavarde'];
			$out .= "</a>";
			$out .= "</td>";
			$out .= "<td>";
			$out .= $row_result['bylosnr'];
			$out .= "</td>";
			
			$out .= "</tr>";

		 }; 
			$out .= "</table><br>";
				
	
};
if (isset($_GET['ismokosid'])){
		$sql = "DELETE FROM `ismokos` WHERE `ismokos`.`ismokosid` = ";
		$sql .= ($_GET['ismokosid']);
		$sql .= " LIMIT 1;";
		
		
		$result = mysql_query($sql, $connection);
			if (!$result) {
				// Display error message.
				$message .= "<p>Migracija table update failed</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			}ELSE{echo "small balls";};
	};
if (isset($_GET['laikotarpis'])){
	
	$laikotarpis = $_GET['laikotarpis'];		
	if (isset($_GET['userid'])){
		$sql = "INSERT INTO `ismokos` (";
		$sql .= "`userid` ,`laikotarpis`) VALUES (";
		$sql .= ($_GET['userid']);
		$sql .= ", '";
		$sql .= ($laikotarpis);
		$sql .= "');";
		
		$result = mysql_query($sql, $connection);
			if (!$result) {
				// Display error message.
				$message .= "<p>Migracija table update failed</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
	};
};	
$query = "SELECT *";
$query .= " FROM `ismokos`, `user`, `lovos`";
$query .= " WHERE `laikotarpis` =";
$query .= "'".$laikotarpis."'";
$query .= " AND `ismokos`.`userid` =";
$query .= " `user`.`userid` ";
$query .= " AND `user`.`lovosid` = ";
$query .= " `lovos`.`lovosid` ";

$result_set = mysql_query($query, $connection);
$count = mysql_num_rows($result_set);
if ($count >= 1) {
$c=1;
			$top = "<table border=\"1\" width=\"100%\">";
			$top .= "<tr>";
			$top .= "<th>Eil.Nr.</th>";
			$top .= "<th>Bylos Nr.</th>";
			$top .= "<th>Name</th>";
			$top .= "<th>Surname</th>";
			$top .= "<th>Gim. data.</th>";
			$top .= "<th>Burys</th>";
			$top .= "<th>Brigada</th>";
			$top .= "<th>Skirta ismoka</th>";
			$top .= "</tr>";
					  
		 while ($row_result = mysql_fetch_assoc($result_set)) { 
			$top .= "<tr";
			if (($row_result['etapas']) == 0) {
				$top .=" bgcolor=\"#DDFFCC\"";
			} elseif (($row_result['etapas']) == 1) {
				$top .=" bgcolor=\"#FFFBC1\"";
			};
			$top .= ">";
			$top .= "<td align=\"center\">";
			$top .= $c++;
			$top .= "</td>";
			$top .= "<td align=\"center\">";
			$top .= $row_result['bylosnr'];
			$top .= "</td>";
			$top .= "<td>";
			//$top .= "<a href=\"ismokos.php?laikotarpis=";
			//$top .= $laikotarpis;
			//$top .= "&userid=";
			//$top .= $row_result['userid'];
			//$top .= "\">";
			$top .= $row_result['vardas'];
			//$top .= "</a>";
			$top .= "</td>";
			$top .= "<td>";
			// $top .= "<a href=\"ismokos.php?userid=";
			// $top .= $row_result['userid'];
			// $top .= "\">";
			$top .= "<a href=\"ismokos.php?laikotarpis=";
			$top .= $laikotarpis;
			$top .= "&ismokosid=";
			$top .= $row_result['ismokosid'];
			$top .= "\">";
			$top .= $row_result['pavarde'];
			$top .= "</a>";
			// $top .= "</a>";
			$top .= "</td>";
			$top .= "<td align=\"center\">";
			$top .= $row_result['gimimodata'];
			$top .= "</td>";
			$top .= "<td align=\"center\">";
			$top .= $row_result['burys'];
			$top .= "</td>";
			$top .= "<td align=\"center\">";
			$top .= $row_result['brigada'];
			$top .= "</td>";
			$top .= "<td align=\"center\">&nbsp;";
			$top .= "</td>";
			$top .= "</tr>";

		 }; 
			$top .= "</table><br>";
};







?>

					<article>
						<h2>Asmenu paieska</h2>
						<p align="center"><strong>PRAVIENIŠKIŲ PATAISOS NAMAI-ATVIROJI KOLONIJA</strong></p>
                      <p>&nbsp;</p>
                      <table width="100%" border="0">
  <tr>
    <td width="70%">&nbsp;</td>
    <td width="30%"><p>TVIRTINU<br>
      Antrosios  valdybos viršininkas</p>
      <br>
      <p>Virginijus  Ceslevičius</p></td>
  </tr>
</table><br>


						<p align="center"><strong>NUTEISTŲJŲ, KURIEMS SKIRTOS PINIGINĖS IŠMOKOS,</strong><br>
						  <strong>SĄRAŠAS</strong></p>
                      <p align="center"><br><?php echo $laikotarpis;?><br>
                          Pravieniškės II</p><br>
<p><?php echo $top;?>
    </p>
	<table width="100%" border="0">
  <tr>
    <td width="70%"><p>Vyresnysis specialistas</p></td>
    <td width="30%" align="right"><p>Gediminas Lukšys</p></td>
  </tr>
</table><br>
	
                      <form autocomplete="off" action="ismokos.php" method="post" name="search">
					    <p>Laikotarpis <input name="laikotarpis" type="date" placeholder="2012-01-31" value="<?php echo $laikotarpis;?>"/>
					    </p><br>
					    <table width="640px" border="1">
					      <tr>
					        <td width="70"> Bylos Nr</td>
					        <td width="150"><input name="bylosnr" type="text" autofocus/></td>
					        <td width="60">Vardas</td>
					        <td width="150"><input name="vardas" type="text" /></td>
					        <td width="60">Pavarde</td>
					        <td width="150"><input name="pavarde" type="text" /></td>
				          </tr>
				        </table>
					    Etape
					    <input name="etape" type="checkbox" id="etapas" value="1">
					    <label for="etape"></label>
					    <p>
					      <input name="Submit" type="submit" value="Paieška"/>
					    </p>
					  
					  <?php echo $out;?>
						</form>
					</article>
				
            
<?php require("includes/footer.php"); ?>					  